﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data.OracleClient;

namespace GatewayInspect.Modules
{
    public partial class BlacklistHistoryForm : PopupForm
    {
        public BlacklistHistoryForm()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            GridBind();
        }

        private void BlacklistHistoryForm_Load(object sender, EventArgs e)
        {
            dtpEnd.Value = DateTime.Now;
            dtpBegin.Value = DateTime.Now.AddMonths(-1);
            GridBind();
        }

        private void GridBind()
        {
            string strSql = "select a.*,b.ORGNAME from T_TRUCK_BLACKLIST a left join T_SYS_ORG b on a.ORGCODE=b.ORGCODE where a.ISDEL = '1' and DELTIME  >= :beginTime and DELTIME  <= :endTime";
            if (!string.IsNullOrWhiteSpace(tbxDriverID.Text))
            {
                strSql += " and DRIVERID like '%" + tbxDriverID.Text + "%'";
            }
            if (!string.IsNullOrWhiteSpace(tbxTruckNo.Text))
            {
                strSql += " and TRUCKNO like '%" + tbxTruckNo.Text + "%'";
            }
            if (cmbOrg.SelectedValue != null)
            {
                strSql += " and a.ORGCODE = '" + cmbOrg.SelectedValue.ToString() + "'";
            }
            this.cdgvBlacklist.AutoGenerateColumns = false;
            this.cdgvBlacklist.DataSource = DataAccess.ExecuteDataTable(strSql, new OracleParameter(":beginTime", dtpBegin.Value), new OracleParameter(":endTime", dtpEnd.Value));
        }

        private void InitQueryFilter()
        {
            //设置库区
            string strSQL = "select ORGCODE, ORGNAME from T_SYS_ORG order by ORGCODE";
            DataTable dt = DataAccess.ExecuteDataTable(strSQL);
            if (dt != null && dt.Rows.Count > 0)
            {
                cmbOrg.DataSource = dt;
                cmbOrg.ValueMember = "ORGCODE";
                cmbOrg.DisplayMember = "ORGNAME";
                cmbOrg.SelectedValue = "";
            }
        }

        #region 读身份证

        private IDCradReader reader = null;

        protected override void OnLoad()
        {
            base.OnLoad();
            InitQueryFilter();
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        protected override void OnClosed()
        {
            base.OnClosed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive && info != null)
            {
                this.tbxDriverID.Text = info.CardNo;
            }
        }

        #endregion
    }
}
